import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:rembercount/services/import_service.dart';

class ImportTestPage extends StatefulWidget {
  const ImportTestPage({Key? key}) : super(key: key);

  @override
  State<ImportTestPage> createState() => _ImportTestPageState();
}

class _ImportTestPageState extends State<ImportTestPage> {
  final ImportService _importService = ImportService();
  bool _isImporting = false;
  String _result = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('CSV导入测试'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      'CSV导入功能测试',
                      style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 16),
                    const Text(
                      '使用说明：\n'
                      '1. 准备CSV文件，包含以下列：收支类型、金额、分类、子分类、记账日期、备注信息\n'
                      '2. 根据平台选择文件获取方式\n'
                      '3. 点击"选择CSV文件"按钮\n'
                      '4. 按照提示选择文件\n'
                      '5. 确认导入',
                      style: TextStyle(fontSize: 14),
                    ),
                    const SizedBox(height: 16),
                    const Text(
                      '示例CSV格式：\n'
                      '收支类型,金额,分类,子分类,记账日期,备注信息\n'
                      '支出,100.00,餐饮,午餐,2024-01-01 12:00,公司午餐\n'
                      '收入,5000.00,工资,月薪,2024-01-01 09:00,1月工资',
                      style: TextStyle(fontSize: 12, color: Colors.grey),
                    ),
                    const SizedBox(height: 16),
                    Container(
                      padding: const EdgeInsets.all(12),
                      decoration: BoxDecoration(
                        color: Colors.blue.withOpacity(0.1),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: const Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '平台支持：',
                            style: TextStyle(
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          SizedBox(height: 8),
                          Text(
                            '• 桌面端：手动输入文件路径\n'
                            '• 移动端：从文件夹选择、从相册选择、拍照、手动输入',
                            style: TextStyle(fontSize: 12),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            const SizedBox(height: 16),
            ElevatedButton.icon(
              onPressed: _isImporting ? null : _importCSV,
              icon: _isImporting 
                ? const SizedBox(
                    width: 16,
                    height: 16,
                    child: CircularProgressIndicator(strokeWidth: 2),
                  )
                : const Icon(Icons.file_upload),
              label: Text(_isImporting ? '导入中...' : '选择CSV文件'),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue,
                foregroundColor: Colors.white,
                padding: const EdgeInsets.symmetric(vertical: 16),
              ),
            ),
            const SizedBox(height: 16),
            if (_result.isNotEmpty)
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '导入结果：',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Text(
                        _result,
                        style: const TextStyle(fontSize: 14),
                      ),
                    ],
                  ),
                ),
              ),
            const Spacer(),
            const Card(
              child: Padding(
                padding: EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '文件获取方法：',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 8),
                    Text(
                      '桌面端：\n'
                      '1. 在Finder中找到CSV文件\n'
                      '2. 右键点击文件\n'
                      '3. 按住Option键，选择"复制文件路径"\n'
                      '4. 或者右键选择"显示简介"，查看位置信息\n'
                      '5. 将完整路径粘贴到对话框中\n\n'
                      '移动端：\n'
                      '1. 从文件夹选择 - 直接浏览文件管理器\n'
                      '2. 从相册选择 - 如果CSV文件已保存到相册\n'
                      '3. 拍照 - 拍摄CSV文件内容\n'
                      '4. 手动输入 - 直接输入文件路径',
                      style: TextStyle(fontSize: 12),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _importCSV() async {
    setState(() {
      _isImporting = true;
      _result = '';
    });

    try {
      var result = await _importService.importCSV();
      
      setState(() {
        _result = result['message'] ?? '导入完成';
        
        if (result['success'] == true) {
          _result += '\n\n成功导入: ${result['successCount']} 条';
          _result += '\n失败: ${result['failCount']} 条';
          
          if (result['errors'] != null && (result['errors'] as List).isNotEmpty) {
            _result += '\n\n错误详情:';
            for (String error in result['errors']) {
              _result += '\n• $error';
            }
          }
        }
      });
    } catch (e) {
      setState(() {
        _result = '导入失败: $e';
      });
    } finally {
      setState(() {
        _isImporting = false;
      });
    }
  }
} 